<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ page import="java.sql.*,myutil.*,java.util.*" %>
<%
String query_string=request.getParameter("query_string").replace("\n"," ");
//System.out.println(query_string);
String[] table_name_array=query_string.split("FROM")[1].split("WHERE");
String table_name="";
if(table_name_array.length==2){
	table_name=table_name_array[0];
}
else{
	table_name=query_string.split("FROM")[1];
}
HashMap<String,String> hm_tables=new HashMap<String,String>();
int column_count=0;
try{
	hm_tables.put("Batch","batch_details");
    hm_tables.put("Course","course_details");
    hm_tables.put("Department","department_details");
    hm_tables.put("Student","student_details");
    hm_tables.put("Hostel","college_residential");
    hm_tables.put("Library","library_book_details");
    hm_tables.put("Placement","placements");
    hm_tables.put("Degree","degree_details");
    hm_tables.put("Staff","college_staff");
    String original_table_name=hm_tables.get(table_name.replaceAll(" ",""));
   System.out.println(table_name+" "+original_table_name);
    query_string=query_string.replace(table_name," "+original_table_name+" ");
	Connect c=new Connect();
	String[] query_from_from=query_string.split("FROM");
	String query_cols=query_from_from[0].split("SELECT")[1].replaceAll(" ","");
	Connect c1=new Connect();
	
	%>
	<table border="1">
	<tr>
	<%
	if(query_cols.equals("*")){
		ResultSet rs1=c1.getResult("desc "+original_table_name);
		while(rs1.next()){
			column_count++;
		out.println("<th>"+rs1.getString("Field")+"</th>");	
		}
	}
	else{
		for(String col:query_cols.replaceAll(" ","").split(",")){
			out.println("<th>"+col+"</th>");
		}
	}
	System.out.println(query_string);
	ResultSet rs=c.getResult(query_string);
	out.println("</tr>");
	while(rs.next()){
		out.println("<tr>");
		if(query_cols.equals("*")){
			for(int i=1;i<=column_count;i++){
				out.println("<td>"+rs.getString(i)+"</td>");
			}
		}
		else{
			for(String col:query_cols.replaceAll(" ","").split(",")){
				out.println("<td>"+rs.getString(col)+"</td>");
			}
		}
		
		out.println("</tr>");
	}
}
	
	catch(Exception e){
		e.printStackTrace();
		out.println("Not a Valid Query");
	}
	%>
	
	
	
	</table>
	
	
	
	




</body>
</html>